package com.oa.pm.service.impl;

import com.joneying.common.annotation.CommonService;
import com.joneying.common.web.response.Resp;
import com.oa.core.enumm.ProcessDefineEnum;
import com.oa.core.exception.BusinessException;
import com.oa.core.exception.DataIsEmptyException;
import com.oa.pm.dto.DimissionManagementAddDTO;
import com.oa.pm.dto.DimissionManagementAuditDTO;
import com.oa.pm.dto.DimissionManagementListDTO;
import com.oa.pm.vo.DimissionManagementDetailsVO;
import com.oa.pm.vo.DimissionManagementListVO;
import com.oa.core.PageResult;
import com.oa.pm.entity.DimissionManagement;
import com.oa.pm.mapper.DimissionManagementMapper;
import com.oa.pm.service.IDimissionManagementService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.oa.system.dto.ApplyApprovalAddDTO;
import com.oa.system.dto.ApprovalDTO;
import com.oa.system.entity.User;
import com.oa.system.service.IApplyApprovalService;
import com.oa.system.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 离职管理表 服务实现类
 * </p>
 *
 * @author syb123
 * @since 2019-01-22
 */
@Service
public class DimissionManagementServiceImpl extends ServiceImpl<DimissionManagementMapper, DimissionManagement> implements IDimissionManagementService {

    private final DimissionManagementMapper dimissionManagementMapper;

    @Autowired
    private IApplyApprovalService applyApprovalService;

    @Autowired
    private IUserService userService;

    @Autowired
    public DimissionManagementServiceImpl(DimissionManagementMapper dimissionManagementMapper) {
        this.dimissionManagementMapper = dimissionManagementMapper;
    }

    @Override
    @CommonService
    public PageResult<DimissionManagement> findList(Integer pageNum, Integer pageSize) {
        if(pageNum == null){
            List<DimissionManagement> list = dimissionManagementMapper.findList();
            return new PageResult<>(list);
        }else{
            // 当前页，总条数 构造 page 对象
            Page<DimissionManagement> page = new Page<>(pageNum, pageSize);
            //查询
            List<DimissionManagement> list = dimissionManagementMapper.findList(page);
            return new PageResult<>(list);
        }
    }

    /**
    * 根据id查询
    */
    @Override
    @CommonService
    public DimissionManagement findDimissionManagementById(String id){
        return dimissionManagementMapper.findById(id);
    }

    @Override
    @CommonService
    public PageResult<DimissionManagementListVO> findDimissionManagementAuditList(DimissionManagementListDTO dimissionManagementListDTO) {
        Page<DimissionManagementListVO> dimissionManagementListVOPage = dimissionManagementListDTO.getPage();
        return new PageResult<>(dimissionManagementMapper.findDimissionManagementAuditList(dimissionManagementListVOPage, dimissionManagementListDTO), dimissionManagementListVOPage.getPages());
    }

    @Transactional
    @Override
    @CommonService
    public Resp audit(DimissionManagementAuditDTO dimissionManagementAuditDTO) throws DataIsEmptyException {
        ApprovalDTO approvalDTO = new ApprovalDTO();
        approvalDTO.setId(Long.valueOf(dimissionManagementAuditDTO.getApplyApprovalId()));
        approvalDTO.setStatus(dimissionManagementAuditDTO.getAuditStatus());

        return applyApprovalService.audit(approvalDTO, d -> {
            DimissionManagement dimissionManagement = new DimissionManagement();
            dimissionManagement.setId(dimissionManagementAuditDTO.getApplyId());
            dimissionManagement.setType(dimissionManagementAuditDTO.getType());
            dimissionManagement.setRemake(dimissionManagementAuditDTO.getRemake());
            dimissionManagement.setLastInService(new Date());
            dimissionManagementMapper.updateById(dimissionManagement);
        });
    }

    @Override
    @CommonService
    public DimissionManagementDetailsVO findDimissionManagementAudit(String id) {
        return dimissionManagementMapper.findDimissionManagementAudit(id);
    }

    @Transactional
    @Override
    @CommonService
    public Resp add(DimissionManagementAddDTO dimissionManagementAddDTO) throws BusinessException, DataIsEmptyException {
        DimissionManagement dimissionManagement = new DimissionManagement();
        BeanUtils.copyProperties(dimissionManagementAddDTO, dimissionManagement);
        if(insert(dimissionManagement)){
            User user = userService.selectById(dimissionManagement.getUserId());

            ApplyApprovalAddDTO applyApprovalAddDTO = new ApplyApprovalAddDTO();
            applyApprovalAddDTO.setApplyId(dimissionManagement.getId());
            applyApprovalAddDTO.setApprovalProcessId(ProcessDefineEnum.RESIGNATION.getId());
            applyApprovalAddDTO.setApplyApprovalName(user.getName()+ ProcessDefineEnum.RESIGNATION.getText());
            applyApprovalAddDTO.setApprovalUserId(dimissionManagement.getUserId());
            return applyApprovalService.addApproval(applyApprovalAddDTO);

        }
        return new Resp().failure("提交失败");
    }
}
